package com.zhaosc.spark.sql.udf

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.DataTypes
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.types.StructField

object UDAFTest {

  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .appName("UDFTest")
      .config("spark.master", "local")
      .getOrCreate();

    val list = "Tom" :: "Tom" :: "Jack" :: "Marry" :: "yarn" :: Nil;

    import spark.implicits._;
    val rdd = spark.sparkContext.parallelize(list)
      .map(v => {
        Row(v)
      })

    val schema = StructType(Seq(StructField("name", DataTypes.StringType)))

    val nameDF = spark.createDataFrame(rdd, schema);
    nameDF.createTempView("student")
    spark.udf.register("nameAppend", NameCountAggregateFunction);

    nameDF.sqlContext.sql("select nameAppend(name) from student").show();

    spark.sparkContext.stop()

  }
}